package com.hjn.spring.aop.afterreturning.aspect;

import com.hjn.spring.aop.afterreturning.pojo.Student;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 测试后置通知的使用以及当中涉及到的参数
 *
 * @auth huaijn@css.com.cn
 * @date 2020/9/14 10:19
 * @version 1.0.0
 */
@Component("myAspect")
@Aspect
public class AfterReturningAspect {

  @AfterReturning(
      value = "execution(* com.hjn.spring.aop.afterreturning.service.*.do*(..))",
      returning = "res")
  public void after(JoinPoint joinPoint, Object res) {
    System.out.println("后置通知执行了" + LocalDateTime.now().toString());
    //    获取执行方法的全路径
    Signature signature = joinPoint.getSignature();
    //    方法名
    String name = signature.getName();
//    如果执行的是添加对象
    if(name.equals("doSaveStudent")){
      res = (Student)res;
      ((Student) res).setAge("5");
      ((Student) res).setName("hjn");
      System.out.println(res);
    }else if(name.equals("doStr")){
      res = "haha";

    }
  }
}
